<?php
//开启会话
session_start();
$adminName = trim($_POST['adminName']);

include_once 'connect.php';

$nub = "select count(id) as shu from leaving";
$res = mysqli_query($connect,$nub);
$leav = mysqli_fetch_array($res);
$shu = $leav['shu'];

$liuyan = "select * from leaving order by id desc";
$resliuyan = mysqli_query($connect,$liuyan);
?>

<?php
include_once 'connect.php';
$sql = "select * from text where adminName = '".$_SESSION['loginadmin']." ' ";
$result = mysqli_query($connect,$sql);
if (mysqli_num_rows($result)){
    $text = mysqli_fetch_array($result);
}else{
    header("Location:login.php");
    die("参数错误");
}
?>

<?php
include_once 'Nav.php';
?>

<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-body">
                <div class="row mb-2">
                    <div class="col-lg-12">
                        <div class="text-lg-right">
                            <h4 class="header-title mb-3 size_18">留言管理<button type="button" class="btn btn-secondary btn-sm btn-rounded margin_left">共<b><?php echo $leav['shu']?></b>条</button>
                            </h4>
                        </div>
                    </div><!-- end col-->
                </div>

                <div class="table-responsive">
                    <table class="table table-centered mb-0">
                        <thead class="thead-light">
                        <tr>
                            <th style="width: 20px;">
                                <div class="custom-control custom-checkbox">
                                    <input type="checkbox" class="custom-control-input" id="customCheck1">
                                    <label class="custom-control-label" for="customCheck1">&nbsp;</label>
                                </div>
                            </th>
                            <th>留言内容</th>
                            <th>Date</th>
                            <th>Name</th>
                            <th>QQ</th>
                            <th>IP</th>
                            <th style="width: 125px;">Action</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php
                        while ($info = mysqli_fetch_array($resliuyan)) {
                        ?>
                        <tr>
                            <td>
                                <div class="custom-control custom-checkbox">
                                    <input type="checkbox" class="custom-control-input" id="customCheck2">
                                    <label class="custom-control-label" for="customCheck2">&nbsp;</label>
                                </div>
                            </td>
                            <td><?php echo $info['text']?></td>
                            <td>
                                <small class="text-muted"><?php echo $info['time']?></small>
                            </td>
                            <td>
                                <h5><span class="badge badge-success-lighten"><i class="mdi mdi-account-circle mr-1 rihjt-0"></i> <?php echo $info['name']?></span></h5>
                            </td>
                            <td>
                                <?php echo $info['QQ']?>
                            </td>
                            <td>
                                <h5><span class="badge badge-danger-lighten"><?php if ($info['ip']) { ?> <?php echo $info['ip'] ?> <?php }else{?>127.0.0.1<?php } ?></span></h5>
                                <i><?php echo get_ip_city($info['ip']) ?> </i>
                            </td>
                            <td>
                                <a href="javascript:del(<?php echo $info['id']; ?>,'<?php echo $info['text'];?>');">
                                    <button style="white-space: nowrap;" type="button" class="btn btn-danger btn-rounded">
                                        <i class=" mdi mdi-delete-empty mr-1"></i>删除</button></a>
                            </td>
                        </tr>
                            <?php
                        }
                        ?>


                        </tbody>
                    </table>
                </div>
            </div> <!-- end card-body-->
        </div> <!-- end card-->
    </div> <!-- end col -->
</div>

<script>
    function del(id,text){
        if (confirm('您确认要删除 '+ text + ' 内容吗')){
            location.href = 'delleav.php?id=' + id + '&text' + text;
        }
    }
</script>

<?php

function get_ip_city($ip){
    $ch = curl_init();
    $url = 'https://whois.pconline.com.cn/ipJson.jsp?ip=' . $ip;
    //用curl发送接收数据
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //请求为https
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $location = curl_exec($ch);
    curl_close($ch);
    //转码
    $location = mb_convert_encoding($location, 'utf-8', 'GB2312');
    //var_dump($location);
    //截取{}中的字符串
    $location = substr($location, strlen('({') + strpos($location, '({'), (strlen($location) - strpos($location, '})')) * (-1));
    //将截取的字符串$location中的‘，’替换成‘&’   将字符串中的‘：‘替换成‘=’
    $location = str_replace('"', "", str_replace(":", "=", str_replace(",", "&", $location)));
    //php内置函数，将处理成类似于url参数的格式的字符串  转换成数组
    parse_str($location, $ip_location);
    return $ip_location['pro'];
}
?>

<?php
include_once 'Footer.php';
?>

</body>
</html>